/*
* Copyright (C) 2020 ~ 2024 chaigec All Rights Reserved.
*
* Author:     chaigec <chaigec@163.com>
* Maintainer: chaigec <chaigec@163.com>

 * Use of this source code is governed by MIT license that can be found in the
 * LICENSE file in the root of the source tree. All contributing project authors
 * may be found in the AUTHORS file in the root of the source tree.
*/

#include "MkUtil/MkFile.h"
#include "MkUtil/MkBase64.h"
#include <gtest/gtest.h>

TEST(MkFile, LogPath)
{
    MkString LogPath;
    Uint32 ErrorCode = CMkFile::GetLogPath(LogPath);
    EXPECT_EQ(ErrorCode, NoneError);
    EXPECT_STRNE(LogPath.c_str(), "");
}

TEST(MkFile, ConfigPath)
{
    MkString ConfigPath;
    Uint32 ErrorCode = CMkFile::GetConfigPath(ConfigPath);
    EXPECT_EQ(ErrorCode, NoneError);
    EXPECT_STRNE(ConfigPath.c_str(), "");
}

TEST(MkFile, DownloadPath)
{
    MkString Download;
    Uint32 ErrorCode = CMkFile::GetDownloadPath(Download);
    EXPECT_EQ(ErrorCode, NoneError);
    EXPECT_STRNE(Download.c_str(), "");
}

TEST(MkFile, FontFilePath)
{
    MkString FontFilePath;
    Uint32 ErrorCode = CMkFile::GetFontFilePath(FontFilePath);
    EXPECT_EQ(ErrorCode, NoneError);
    EXPECT_STRNE(FontFilePath.c_str(), "");
}

TEST(MkFile, Files)
{
    MkString ConfigPath;
    Uint32 ErrorCode = CMkFile::GetConfigPath(ConfigPath);
    EXPECT_EQ(ErrorCode, NoneError);
    MkString TestFilePath = ConfigPath + "/test";
    CMkFile::CreateBasePath(TestFilePath);
    BOOL bTmp = CMkFile::IsDir(TestFilePath);
    EXPECT_EQ(TRUE, bTmp);
    bTmp = CMkFile::CheckDirIsExist(TestFilePath);
    EXPECT_EQ(TRUE, bTmp);
    MkString TestBaseFile = TestFilePath + "/test.txt";
    MkString strTest = "123";
    MkString strBase64;
    MkBase64Encode(strTest, strBase64);
    ErrorCode = CMkFile::WriteBase64FileBuffer(TestBaseFile, strBase64);
    EXPECT_EQ(ErrorCode, NoneError);
    MkString TestFile = TestFilePath + "/test2.txt";
    ErrorCode = CMkFile::WriteFileBuffer(TestFile, strTest);
    EXPECT_EQ(ErrorCode, NoneError);
    MkString strRead;
    ErrorCode = CMkFile::ReadFileBuffer(TestFile, strRead);
    EXPECT_EQ(ErrorCode, NoneError);
    EXPECT_STREQ(strRead.c_str(), strTest.c_str());
    strRead.clear();
    ErrorCode = CMkFile::ReadFileBuffer(TestBaseFile, strRead);
    EXPECT_EQ(ErrorCode, NoneError);
    EXPECT_STREQ(strRead.c_str(), strTest.c_str());
    Uint32 nSize = CMkFile::GetFileSize(TestFile);
    EXPECT_EQ(strTest.size(), nSize);
    MkString BackFile = TestFilePath + "/back.zip";
    ErrorCode = CMkFile::BackUpFile(TestBaseFile, BackFile);
    EXPECT_EQ(ErrorCode, NoneError);

    MkVectorString vecFiles;
    ErrorCode = CMkFile::ScanDir(TestFilePath, vecFiles);
    EXPECT_EQ(ErrorCode, NoneError);
    EXPECT_EQ(3, vecFiles.size());
    MkVectorString::iterator it = vecFiles.begin();
    for (; it != vecFiles.end(); it++) {
        CMkFile::DeleteLocalFile(*it);
    }

    CMkFile::RemoveDir(TestFilePath);
}

TEST(MkFile, GetFileSizeError)
{
    Uint32 FileSize = CMkFile::GetFileSize("aaa/bbb/ccc.dat");
    EXPECT_EQ(0, FileSize);
}

TEST(MkFile, NoneFileContent)
{
    MkString Content;
    Uint32 ErrorCode = CMkFile::ReadFileBuffer("aaa/bbb/ccc.dat",Content);
    EXPECT_NE(NoneError, ErrorCode);

    EXPECT_EQ(true, Content.empty());
}

TEST(MkFile, WriteBufferFailed)
{
    MkString Content;
    Uint32 ErrorCode = CMkFile::WriteFileBuffer("aaa/bbb/ccc.dat", Content);
    EXPECT_NE(NoneError, ErrorCode);
}

TEST(MkFile, CheckEmptyString)
{
    BOOL bRet = CMkFile::CheckFileIsExist("");
    EXPECT_EQ(FALSE, bRet);
}

TEST(MkFile, ScanNotDir)
{
    MkVectorString VecChild;
    Uint32 ErrorCode = CMkFile::ScanDir("aa/bb", VecChild);
    EXPECT_NE(NoneError, ErrorCode);
    EXPECT_EQ(true, VecChild.empty());
}

